package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.modles.CommentsListData;
import com.wangtao.firstapp.utils.CommentsDialogUtils;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 评论页面 的适配器
 * Created by lanouhn on 16/9/13.
 */
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.CommentsViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private Context context;
    private CommentsListData commentsListData;

    //创建自定义Dialog对象，并实例化
    private CommentsDialogUtils commentsDialogUtils = new CommentsDialogUtils();

    public CommentsAdapter(Context context, CommentsListData commentsListData) {
        this.context = context;
        this.commentsListData = commentsListData;
    }

    @Override
    public CommentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_act_comments, null);
        CommentsViewHolder holder = new CommentsViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        return holder;
    }

    @Override
    public void onBindViewHolder(CommentsViewHolder holder, final int position) {

        Picasso.with(context).load(commentsListData.getData().get(position).getUser().getAvatar()).into(holder.logo);
        holder.name.setText(commentsListData.getData().get(position).getUser().getNickname());
        holder.time.setText(commentsListData.getData().get(position).getDatestr());
        holder.content.setText(commentsListData.getData().get(position).getConent());

        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取作者ID
                String author_ID = commentsListData.getData().get(position).getUser().getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);
            }
        });

        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 获取作者ID
                String author_ID = commentsListData.getData().get(position).getUser().getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);

            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                AlertDialog.Builder aBuilder=new AlertDialog.Builder(context);
//                View view=LayoutInflater.from(context).inflate(R.layout.comments_dialog,null);
//                aBuilder.setView(view);
//                aBuilder.setCancelable(false);
//                AlertDialog alertDialog = aBuilder.create();
//
//                TextView reply= (TextView) view.findViewById(R.id.reply_comments_dialog);//回复
//                TextView copy= (TextView) view.findViewById(R.id.copy_comments_dialog);//复制
//                TextView callof= (TextView) view.findViewById(R.id.callof_comments_dialog);//取消
//
//                reply.setOnClickListener(this);
//                copy.setOnClickListener(this);
//                callof.setOnClickListener(this);
//
//                alertDialog.show();


                commentsDialogUtils.commentsDialog(context,cdonClickListener);
            }
        });
    }

    private View.OnClickListener cdonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                //回复
                case R.id.reply_comments_dialog:
                    commentsDialogUtils.replyCD();
                    break;

                //复制
                case R.id.copy_comments_dialog:
                    commentsDialogUtils.copyCD();
                    break;
                //取消
                case R.id.callof_comments_dialog:
                    commentsDialogUtils.callofCD();
                    break;
            }
        }
    };

    @Override
    public int getItemCount() {
        return commentsListData.getData().size();
    }

    public class CommentsViewHolder extends RecyclerView.ViewHolder {

        private ImageView logo;
        private TextView name, time, content;

        public CommentsViewHolder(View itemView) {
            super(itemView);
            logo = (CircleImageView) itemView.findViewById(R.id.logo_item_act_comments);//作者头像
            name = (TextView) itemView.findViewById(R.id.name_item_act_comments);//作者昵称
            time = (TextView) itemView.findViewById(R.id.time_item_act_comments);//评论时间
            content = (TextView) itemView.findViewById(R.id.content_item_act_comments);//评论内容

        }
    }
}
